package com.kwin.demo.server.module.thread.jointhread;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author kwin
 * @Date 2022/8/22 16:25
 **/
public class Test {

//    public static final ThreadLocal<String> THREAD_LOCAL_0 = new ThreadLocal<>();
    public static final ThreadLocal<String> THREAD_LOCAL = new TransmittableThreadLocal();
    public static Executor executorService = null;

    static ExecutorService build() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        // 核心线程池大小
//        executor.setCorePoolSize(5);
//        executor.setQueueCapacity(100);
//        executor.initialize();
        ExecutorService executorS = Executors.newFixedThreadPool(1);
        return TtlExecutors.getTtlExecutorService(executorS);
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {

        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");

        ExecutorService executor = build();

        List<Future> rets = new ArrayList<>();
        for(String str : list) {
            THREAD_LOCAL.set(str);
            System.out.println(THREAD_LOCAL.get());
            Future future = executor.submit(() -> {
                System.out.println("-------------------------------" + str + "-----------------------");
                System.out.println(THREAD_LOCAL.get());
            });
            rets.add(future);
        }

        for(Future future : rets) {
            future.get(1000, TimeUnit.MILLISECONDS);
        }


//        System.out.println(THREAD_LOCAL.get());
//        System.out.println(THREAD_LOCAL_0.get());
       /* new Thread(() -> {
            THREAD_LOCAL.set("1");
//            THREAD_LOCAL.remove();
//            THREAD_LOCAL_0.set("01233210");
            System.out.println("-------------------1--------------");
            System.out.println(THREAD_LOCAL.get());
//            System.out.println(THREAD_LOCAL_0.get());

            executorService.submit(() -> {
                System.out.println("-------------------1.1--------------");
                System.out.println(THREAD_LOCAL.get());
//                System.out.println(THREAD_LOCAL_0.get());
            });
            *//*try {
                future.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            THREAD_LOCAL.remove();*//*
            *//*new Thread(() -> {
                System.out.println("-------------------2--------------");
                System.out.println(THREAD_LOCAL.get());
                System.out.println(THREAD_LOCAL_0.get());
            }).start();*//*
        }).start();

        new Thread(() -> {
            System.out.println("-------------------2--------------");
            THREAD_LOCAL.set("2");
            System.out.println(THREAD_LOCAL.get());
//            System.out.println(THREAD_LOCAL_0.get());
            executorService.submit(() -> {
                System.out.println("-------------------2.1--------------");
                System.out.println(THREAD_LOCAL.get());
//                System.out.println(THREAD_LOCAL_0.get());
            });
//            executorService.submit(() -> {
//                System.out.println("-------------------1.3--------------");
//                System.out.println(THREAD_LOCAL.get());
//                System.out.println(THREAD_LOCAL_0.get());
//            });
        }).start();

        new Thread(() -> {
            THREAD_LOCAL.set("3");
            System.out.println("-------------------3--------------");
            System.out.println(THREAD_LOCAL.get());
//            System.out.println(THREAD_LOCAL_0.get());
            executorService.submit(() -> {
                System.out.println("-------------------3.1--------------");
                System.out.println(THREAD_LOCAL.get());
                //                System.out.println(THREAD_LOCAL_0.get());
            });
            //            executorService.submit(() -> {
            //                System.out.println("-------------------1.3--------------");
            //                System.out.println(THREAD_LOCAL.get());
            //                System.out.println(THREAD_LOCAL_0.get());
            //            });
        }).start();*/
    }
}
